home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / berkeleydb1.73 / Berkeley_db / btree / bt_conv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-27  |  5.2 KB  |  226 lines  |  [TEXT/MPS ]

  1. /*-
  2.  * Copyright (c) 1990, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Mike Olson.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)bt_conv.c    8.1 (Berkeley) 6/4/93";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. #if defined(macintosh) && (defined(powerc) || defined(__powerc))
  42. #include "OurMalloc.h"
  43. #endif
  44.  
  45. #include <sys/param.h>
  46.  
  47. #include <stdio.h>
  48.  
  49. #include <db.h>
  50. #include "btree.h"
  51.  
  52. static void mswap __P((PAGE *));
  53.  
  54. /*
  55.  * __BT_BPGIN, __BT_BPGOUT --
  56.  *    Convert host-specific number layout to/from the host-independent
  57.  *    format stored on disk.
  58.  *
  59.  * Parameters:
  60.  *    t:    tree
  61.  *    pg:    page number
  62.  *    h:    page to convert
  63.  */
  64. void
  65. __bt_pgin(t, pg, pp)
  66.     void *t;
  67.     pgno_t pg;
  68.     void *pp;
  69. {
  70.     PAGE *h;
  71.     int i, top;
  72.     u_char flags;
  73.     char *p;
  74.  
  75.     if (!ISSET(((BTREE *)t), B_NEEDSWAP))
  76.         return;
  77.     if (pg == P_META) {
  78.         mswap(pp);
  79.         return;
  80.     }
  81.  
  82.     h = pp;
  83.     BLSWAP(h->pgno);
  84.     BLSWAP(h->prevpg);
  85.     BLSWAP(h->nextpg);
  86.     BLSWAP(h->flags);
  87.     BSSWAP(h->lower);
  88.     BSSWAP(h->upper);
  89.  
  90.     top = NEXTINDEX(h);
  91.     if ((h->flags & P_TYPE) == P_BINTERNAL)
  92.         for (i = 0; i < top; i++) {
  93.             BSSWAP(h->linp[i]);
  94.             p = (char *)GETBINTERNAL(h, i);
  95.             BLPSWAP(p);
  96.             p += sizeof(size_t);
  97.             BLPSWAP(p);
  98.             p += sizeof(pgno_t);
  99.             if (*(u_char *)p & P_BIGKEY) {
  100.                 p += sizeof(u_char);
  101.                 BLPSWAP(p);
  102.                 p += sizeof(pgno_t);
  103.                 BLPSWAP(p);
  104.             }
  105.         }
  106.     else if ((h->flags & P_TYPE) == P_BLEAF)
  107.         for (i = 0; i < top; i++) {
  108.             BSSWAP(h->linp[i]);
  109.             p = (char *)GETBLEAF(h, i);
  110.             BLPSWAP(p);
  111.             p += sizeof(size_t);
  112.             BLPSWAP(p);
  113.             p += sizeof(size_t);
  114.             flags = *(u_char *)p;
  115.             if (flags & (P_BIGKEY | P_BIGDATA)) {
  116.                 p += sizeof(u_char);
  117.                 if (flags & P_BIGKEY) {
  118.                     BLPSWAP(p);
  119.                     p += sizeof(pgno_t);
  120.                     BLPSWAP(p);
  121.                 }
  122.                 if (flags & P_BIGDATA) {
  123.                     p += sizeof(size_t);
  124.                     BLPSWAP(p);
  125.                     p += sizeof(pgno_t);
  126.                     BLPSWAP(p);
  127.                 }
  128.             }
  129.         }
  130. }
  131.  
  132. void
  133. __bt_pgout(t, pg, pp)
  134.     void *t;
  135.     pgno_t pg;
  136.     void *pp;
  137. {
  138.     PAGE *h;
  139.     int i, top;
  140.     u_char flags;
  141.     char *p;
  142.  
  143.     if (!ISSET(((BTREE *)t), B_NEEDSWAP))
  144.         return;
  145.     if (pg == P_META) {
  146.         mswap(pp);
  147.         return;
  148.     }
  149.  
  150.     h = pp;
  151.     top = NEXTINDEX(h);
  152.     if ((h->flags & P_TYPE) == P_BINTERNAL)
  153.         for (i = 0; i < top; i++) {
  154.             p = (char *)GETBINTERNAL(h, i);
  155.             BLPSWAP(p);
  156.             p += sizeof(size_t);
  157.             BLPSWAP(p);
  158.             p += sizeof(pgno_t);
  159.             if (*(u_char *)p & P_BIGKEY) {
  160.                 p += sizeof(u_char);
  161.                 BLPSWAP(p);
  162.                 p += sizeof(pgno_t);
  163.                 BLPSWAP(p);
  164.             }
  165.             BSSWAP(h->linp[i]);
  166.         }
  167.     else if ((h->flags & P_TYPE) == P_BLEAF)
  168.         for (i = 0; i < top; i++) {
  169.             p = (char *)GETBLEAF(h, i);
  170.             BLPSWAP(p);
  171.             p += sizeof(size_t);
  172.             BLPSWAP(p);
  173.             p += sizeof(size_t);
  174.             flags = *(u_char *)p;
  175.             if (flags & (P_BIGKEY | P_BIGDATA)) {
  176.                 p += sizeof(u_char);
  177.                 if (flags & P_BIGKEY) {
  178.                     BLPSWAP(p);
  179.                     p += sizeof(pgno_t);
  180.                     BLPSWAP(p);
  181.                 }
  182.                 if (flags & P_BIGDATA) {
  183.                     p += sizeof(size_t);
  184.                     BLPSWAP(p);
  185.                     p += sizeof(pgno_t);
  186.                     BLPSWAP(p);
  187.                 }
  188.             }
  189.             BSSWAP(h->linp[i]);
  190.         }
  191.  
  192.     BLSWAP(h->pgno);
  193.     BLSWAP(h->prevpg);
  194.     BLSWAP(h->nextpg);
  195.     BLSWAP(h->flags);
  196.     BSSWAP(h->lower);
  197.     BSSWAP(h->upper);
  198. }
  199.  
  200. /*
  201.  * MSWAP -- Actually swap the bytes on the meta page.
  202.  *
  203.  * Parameters:
  204.  *    p:    page to convert
  205.  */
  206. static void
  207. mswap(pg)
  208.     PAGE *pg;
  209. {
  210.     char *p;
  211.  
  212.     p = (char *)pg;
  213.     BLPSWAP(p);        /* m_magic */
  214.     p += sizeof(u_long);
  215.     BLPSWAP(p);        /* m_version */
  216.     p += sizeof(u_long);
  217.     BLPSWAP(p);        /* m_psize */
  218.     p += sizeof(u_long);
  219.     BLPSWAP(p);        /* m_free */
  220.     p += sizeof(u_long);
  221.     BLPSWAP(p);        /* m_nrecs */
  222.     p += sizeof(u_long);
  223.     BLPSWAP(p);        /* m_flags */
  224.     p += sizeof(u_long);
  225. }
  226.